package com.pcg.posgasstation.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.pcg.posgasstation.App;
import com.pcg.posgasstation.R;
import com.pcg.posgasstation.base.BaseActivity;
import com.pcg.posgasstation.model.cache.CacheManager;
import com.pcg.posgasstation.model.network.ApiClient;
import com.pcg.posgasstation.model.network.entity.BaseEntity;
import com.pcg.posgasstation.model.network.entity.LoginEntity;
import com.pcg.posgasstation.utils.LogUtil;
import com.pcg.posgasstation.utils.ToastUtils;
import com.pcg.posgasstation.utils.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.edtLoginName)
    EditText mEdtLoginName;
    @BindView(R.id.edtLoginPsw)
    EditText mEdtLoginPsw;
    @BindView(R.id.btnLogin)
    Button mBtnLogin;

    private static final String TAG = LoginActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        initViews();
    }

    @OnClick(R.id.btnLogin)
    void btnLoginClick() {
        String loginName = mEdtLoginName.getText().toString();

        if (TextUtils.isEmpty(loginName)) {
            ToastUtils.showToast(getString(R.string.input_user_name));
            return;
        }

        String password = mEdtLoginPsw.getText().toString();

        if (TextUtils.isEmpty(password)) {
            ToastUtils.showToast(getString(R.string.input_password));
            return;
        }

        login(loginName, password);
    }

    private void login(String name, String psw) {
        mProgressDialog.show();
        ApiClient.getApiService().login(App.getInstance().getDeviceId(), name,
                Utils.encryptionPassword(psw))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity<LoginEntity>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        mDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseEntity<LoginEntity> entity) {
                        mProgressDialog.hide();
                        if (entity != null && entity.isSuccess()) {
                            LogUtil.e(TAG,"login success");
                            LoginEntity loginEntity = entity.getData();
                            if (loginEntity != null) {
                                String token = loginEntity.getToken();
                                ApiClient.setToken(token);
                                CacheManager.getInstance().setLoginedToken(token);
                                startMainActivity();
                            }
                        } else {
                            LogUtil.e(TAG,"login failed");
                            ToastUtils.showToast(entity.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mProgressDialog.hide();
                        e.printStackTrace();
                        LogUtil.e(TAG, "login onError = " + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void initViews() {
        mProgressDialog = new ProgressDialog(this);
        String token = CacheManager.getInstance().getLoginedToken();
        if (!TextUtils.isEmpty(token)) {
            ApiClient.setToken(token);
            startMainActivity();
        }
    }

    private void startMainActivity() {
        MainActivity.startActivity(mContext);
        finish();
    }

}
